/*
 * @Author: szx
 * @Date: 2022-03-23 14:21:09
 * @LastEditTime: 2022-03-23 14:37:32
 * @Description:
 * @FilePath: \leetcode\剑指offer\34\34.js
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} target
 * @return {number[][]}
 */
var pathSum = function (root, target) {
    if (!root) return [];
    const res = [];
    const backtrace = (root, path, target) => {
        path.push(root.val);
        if (!root.left && !root.right && target == root.val) res.push(Array.from(path));
        root.left && backtrace(root.left, path, target - root.val);
        root.right && backtrace(root.right, path, target - root.val);
        path.pop();
    };
    backtrace(root, [], target);
    return res;
};
